//
//  PlayViewController.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/5/4.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "PlayViewController.h"
#import "SongListSongsDetailModel.h"
#import "SongListSongsModel.h"

@interface PlayViewController ()

@property (nonatomic, retain) YHYBaseLabel *songLabel;

@property (nonatomic, retain) YHYBaseLabel *singerLabel;

@property (nonatomic, retain) UIImageView *imageView;

@property (nonatomic, retain) UIImageView *controllImage;

@property (nonatomic, retain) YHYBaseView *playView;

@property (nonatomic, retain) AVPlayerItem *item;

@property (nonatomic, assign) playStyle style;

@property (nonatomic, retain) UIButton *loadButton;/**< 下载 */

@property (nonatomic, retain) UIButton *favoriteButton;/**< 收藏 */

@property (nonatomic, retain) UIButton *shareButton;/**< 分享 */

@property (nonatomic, retain) UISlider *slider;/**< 播放进度 */

@property (nonatomic, retain) UIButton *mvButton;/**< mv */

@property (nonatomic, retain) UIButton *typeButton;/**< 类型 */

@property (nonatomic, retain) UIButton *listButton;/**< 列表 */

@property (nonatomic, retain) UIButton *playButton;/**< 播放 */

@property (nonatomic, retain) UIButton *nextSongButton;/**< 下一曲 */

@property (nonatomic, retain) UIButton *beforeSongButton;/**< 上一曲 */

@property (nonatomic, retain) NSTimer *sliderTimer;/**< 监测slider进度时间 */

@property (nonatomic, retain) NSTimer *timer;/**< 观察时间 */

@property (nonatomic, retain) YHYBaseLabel *currentTimeLabel;/**< 当前时间 */

@property (nonatomic, retain) YHYBaseLabel *wholeTimeLabel;/**< 总时间 */

@property (nonatomic, retain) NSString *timeStr;/**< 当前时间 */

@property (nonatomic, retain) NSString *timeStr1;/**< 总时间 */

@property (nonatomic, retain) YHY_DataBaseTool *dataBase;



@end

@implementation PlayViewController

+ (instancetype)SharePlayViewController {
    
    static PlayViewController *playVC = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        playVC = [[PlayViewController alloc] init];
        
    });
    
    return playVC;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:1.00 green:0.71 blue:0.64 alpha:1.00];
    
    
    
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        /**< 背景图 */
        self.controllImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
        _controllImage.image = [UIImage imageNamed:@"音乐背景"];
        _controllImage.userInteractionEnabled = YES;
        [self.view addSubview:_controllImage];
        
        
        /**< 歌曲名 */
        self.songLabel = [[YHYBaseLabel alloc] initWithFrame:CGRectMake(50 * FitWidth, 30 * FitHeight, 200 * FitWidth, 30 * FitHeight)];
        _songLabel.font = [UIFont boldSystemFontOfSize:20];
        _songLabel.textColor = [UIColor whiteColor];
        _songLabel.textAlignment = NSTextAlignmentCenter;
        [self.controllImage addSubview:_songLabel];
        
        /**< 歌手名 */
        self.singerLabel = [[YHYBaseLabel alloc] initWithFrame:CGRectMake(50 * FitWidth, 65 * FitHeight, 200 * FitWidth, 20 * FitHeight)];
        _singerLabel.textColor = [UIColor whiteColor];
        _singerLabel.textAlignment = NSTextAlignmentCenter;
        _singerLabel.font = [UIFont boldSystemFontOfSize:17];
        [self.controllImage addSubview:_singerLabel];
        
        /**< 播放背景图 */
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x - (WIDTH - 140) / 2, self.view.center.y - 200, WIDTH - 140, WIDTH - 140)];
        _imageView.layer.masksToBounds = YES;
        _imageView.layer.cornerRadius = (WIDTH - 140) / 2;
        _imageView.userInteractionEnabled = YES;
        
        [self.controllImage addSubview:_imageView];
        
        /**< 返回上一页 */
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(20, 20, 30, 30);
        [button setImage:[[UIImage imageNamed:@"musicBack"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.controllImage addSubview:button];
        
        [self creatPlayView];
        
    }
    return self;
}

- (void)buttonAction:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}


#pragma mark - ------------ 创建播放视图 ---------------
- (void)creatPlayView {
    
    /**< 创建播放的背景View */
    YHYBaseView *playView = [[YHYBaseView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 200, WIDTH, 200)];
    playView.backgroundColor = [UIColor clearColor];
    playView.alpha = 0.5;
    [self.controllImage addSubview:playView];
    
    
    /**< 下载 */
    self.loadButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _loadButton.frame = CGRectMake((WIDTH - 120) / 5, 7, 30 * FitWidth, 30 * FitHeight);
    [_loadButton setImage:[[UIImage imageNamed:@"musiDownLoad"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_loadButton addTarget:self action:@selector(loadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_loadButton];
    
    
    /**< 收藏 */
    self.favoriteButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _favoriteButton.frame = CGRectMake((WIDTH - 120) / 5 * 2 + 30, 7, 30 * FitWidth, 30 * FitHeight);
    [_favoriteButton addTarget:self action:@selector(favoriteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_favoriteButton setImage:[[UIImage imageNamed:@"收藏前"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [playView addSubview:_favoriteButton];
    
    
    /**< 分享 */
    self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _shareButton.frame = CGRectMake((WIDTH - 120) / 5 * 3 + 60, 7, 30 * FitWidth, 30 * FitHeight);
    [_shareButton setImage:[[UIImage imageNamed:@"musicSharre"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_shareButton];


    /**< MV */
    self.mvButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _mvButton.frame = CGRectMake((WIDTH - 120) / 5 * 4 + 90, 7, 30 * FitWidth, 30 * FitHeight);
    [_mvButton setImage:[[UIImage imageNamed:@"musicMV"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_mvButton addTarget:self action:@selector(mvButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_mvButton];
    
    
    /**< 当前播放时间 */
    self.currentTimeLabel = [[YHYBaseLabel alloc] initWithFrame:CGRectMake(10, 45, 40, 30)];
    _currentTimeLabel.textColor = [UIColor blueColor];
    _currentTimeLabel.font = [UIFont systemFontOfSize:13];
    [playView addSubview:_currentTimeLabel];
    
    
    /**< 总时间 */
    self.wholeTimeLabel = [[YHYBaseLabel alloc] initWithFrame:CGRectMake(WIDTH - 40, 45, 40, 30)];
    _wholeTimeLabel.font = [UIFont systemFontOfSize:13];
    _wholeTimeLabel.textColor = [UIColor blueColor];
    [playView addSubview:_wholeTimeLabel];
    
    
    /**< NSTimer 与 Slider 关联 */
    self.sliderTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
    
    
    /**< NSTimer 观察时间 播放 */
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(observePlay) userInfo:nil repeats:YES];

    
    /**< 进度条 */
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 45, WIDTH - 100, 30)];
    _slider.minimumValue = 0.0f;
    _slider.maximumValue = 1.0f;
    [_slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [playView addSubview:_slider];

    
    /**< 播放模式 */
    self.style = [MusicAVPlayer shareMusicAVPlayer].style;
    self.typeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _typeButton.frame = CGRectMake(20, _loadButton.frame.origin.y + 93, 35 * FitWidth, 35 * FitHeight);
    
    /**< 顺序 */
    if ([MusicAVPlayer shareMusicAVPlayer].style == 0) {
        
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"列表循环"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
    }
    /**< 单曲 */
    if ([MusicAVPlayer shareMusicAVPlayer].style == 1) {
        
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"musicSuiji"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
                
    }
    /**< 随机 */
    if ([MusicAVPlayer shareMusicAVPlayer].style == 2) {
        
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"musicOnly"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
    }
    
    [_typeButton addTarget:self action:@selector(typeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_typeButton];

    
    /**< 播放 / 暂停 */
    self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _playButton.frame = CGRectMake(WIDTH / 2 - 45, _typeButton.frame.origin.y - 19, 90 * FitWidth, 90 * FitHeight);
    if ([MusicAVPlayer shareMusicAVPlayer].isPlay) {
        
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"musicStop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self imageRound];
        [MusicAVPlayer shareMusicAVPlayer].isPlay = YES;
        
    } else {
        
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"musicPlay"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self imageRound];
        [MusicAVPlayer shareMusicAVPlayer].isPlay = NO;
        
    }
    [_playButton addTarget:self action:@selector(playAndStopButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_playButton];
    
    /**< 上一曲 */
    self.beforeSongButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _beforeSongButton.frame = CGRectMake((WIDTH - 220) / 6 + 64, _loadButton.frame.origin.y + 93, 35 * FitWidth, 35 * FitHeight);
    [_beforeSongButton setImage:[[UIImage imageNamed:@"musicLastSong"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_beforeSongButton addTarget:self action:@selector(beforeSongButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_beforeSongButton];

    
    /**< 下一曲 */
    self.nextSongButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _nextSongButton.frame = CGRectMake(WIDTH - (WIDTH - 220) / 6 - 98, _loadButton.frame.origin.y + 93, 35 * FitWidth, 35 * FitHeight);
    [_nextSongButton setImage:[[UIImage imageNamed:@"musicNext"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_nextSongButton addTarget:self action:@selector(nextSongButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_nextSongButton];


    
    /**< 播放列表 */
    self.listButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _listButton.frame = CGRectMake(WIDTH - 55, _typeButton.frame.origin.y, 35 * FitWidth, 35 * FitHeight);
    [_listButton setImage:[[UIImage imageNamed:@"musicList"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_listButton addTarget:self action:@selector(listButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_listButton];

    
}

#pragma mark - ------------ 下载 ---------------
- (void)loadButtonAction:(UIButton *)sender {
    
}

#pragma mark - ------------ 收藏 ---------------
- (void)favoriteButtonAction:(UIButton *)sender {
    
}

#pragma mark - ------------ 分享 ---------------
- (void)shareButtonAction:(UIButton *)sender {
    
}

#pragma mark - ------------ MV ---------------
- (void)mvButtonAction:(UIButton *)sender {
    
}

#pragma mark - ------------ 监听进度条事件 ---------------
- (void)sliderAction:(UIButton *)sender {
    
    //    参数1:跳转到的时间
    //    参数2:每秒多少帧（fps）
    /**< 只有音乐播放时slider可以动 */
    if ([MusicAVPlayer shareMusicAVPlayer].currentItem.duration.value) {
        [[MusicAVPlayer shareMusicAVPlayer] seekToTime:(CMTimeMakeWithSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.duration.value / [MusicAVPlayer shareMusicAVPlayer].currentItem.duration.timescale * self.slider.value, [MusicAVPlayer shareMusicAVPlayer].currentItem.duration.timescale)) completionHandler:^(BOOL finished) {
            
            [[MusicAVPlayer shareMusicAVPlayer] play];
            
        }];
    }
    
}

#pragma mark - ------------ NSTimer 与 Slider 关联 ---------------
- (void)updateSlider {
    
    self.slider.value = CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.currentTime) / CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.duration);
    
    /**< 总时间 */
    NSInteger totalTime = CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.duration);
    NSInteger minTime = totalTime / 60;
    NSInteger secondTime = totalTime % 60;
    _timeStr = [NSString stringWithFormat:@"%02ld:%02ld", (long)minTime,(long)secondTime];
    
    /**< 当前时间 */
    NSInteger totalTime1 = CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.currentTime);
    NSInteger minTime1 = totalTime1 / 60;
    NSInteger secondTime1 = totalTime1 % 60;
    _timeStr1 = [NSString stringWithFormat:@"%02ld:%02ld", (long)minTime1,(long)secondTime1];
    
    if(![_timeStr isEqualToString:@""] && ![_timeStr1 isEqualToString:@""]) {
        _currentTimeLabel.text = _timeStr1;
        _wholeTimeLabel.text = _timeStr;
    }
    
}

#pragma mark - ------------ 观察时间，当一首歌播完，会播放下一首 ----------
- (void)observePlay {
    
    if (CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.currentTime) / CMTimeGetSeconds([MusicAVPlayer shareMusicAVPlayer].currentItem.duration) == 1) {
        
        /**< 列表循环 */
        if ([MusicAVPlayer shareMusicAVPlayer].style == 0) {
            [MusicAVPlayer shareMusicAVPlayer].local += 1;
        }
        if ([MusicAVPlayer shareMusicAVPlayer].style == 1) {
    
        }
        if ([MusicAVPlayer shareMusicAVPlayer].style == 2) {
            [MusicAVPlayer shareMusicAVPlayer].local = arc4random()%_songDetailModel.songs.count;
        }
        [[MusicAVPlayer shareMusicAVPlayer] playWithURL:_songDetailModel];
        [MusicAVPlayer shareMusicAVPlayer].style = _style;
        
    }
    
}

#pragma mark - ------------ 播放类型 ---------------
- (void)typeButtonAction:(UIButton *)sender {
    
    if (_style == 2) {
        _style = 0;
    }else {
        _style += 1;
    }
    
    /**< 顺序 */
    if (_style == 0) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"列表循环"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    if (_style == 1) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"musicOnly"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    if (_style == 2) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"musicSuiji"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
}

#pragma mark - ------------ 播放 / 暂停 ---------------
- (void)playAndStopButtonAction:(UIButton *)sender {
    
    if ([MusicAVPlayer shareMusicAVPlayer].isPlay == YES) {
        [[MusicAVPlayer shareMusicAVPlayer]pause];
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"musicPlay"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [MusicAVPlayer shareMusicAVPlayer].isPlay = NO;
        [self imageRound];
        [_imageView.layer removeAllAnimations];
    }else {
        [[MusicAVPlayer shareMusicAVPlayer]play];
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"musicStop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self imageRound];
        [MusicAVPlayer shareMusicAVPlayer].isPlay = YES;
    }
    
}

#pragma mark - ------------ 上一曲 ---------------
- (void)beforeSongButtonAction {
    
    /**< 顺序 */
    if ([MusicAVPlayer shareMusicAVPlayer].style == 0) {
        
        if ([MusicAVPlayer shareMusicAVPlayer].local == 0) {
            [MusicAVPlayer shareMusicAVPlayer].local = _songDetailModel.songs.count - 1;
        }else {
            [MusicAVPlayer shareMusicAVPlayer].local -= 1;
        }
//        [[MusicAVPlayer shareMusicAVPlayer]play];
        [MusicAVPlayer shareMusicAVPlayer].style = _style;
    }
    /**< 单曲 */
    if ([MusicAVPlayer shareMusicAVPlayer].style == 1) {
        if ([MusicAVPlayer shareMusicAVPlayer].local == 0) {
            [MusicAVPlayer shareMusicAVPlayer].local = _songDetailModel.songs.count - 1;
        }else {
            [MusicAVPlayer shareMusicAVPlayer].local -= 1;
        }
//        [[MusicAVPlayer shareMusicAVPlayer]play];
        [MusicAVPlayer shareMusicAVPlayer].style = _style;
    }
    /**< 随机 */
    if ([MusicAVPlayer shareMusicAVPlayer].style == 2) {
        [MusicAVPlayer shareMusicAVPlayer].local = arc4random()% _songDetailModel.songs.count;
//        [[MusicAVPlayer shareMusicAVPlayer]play];
        [MusicAVPlayer shareMusicAVPlayer].style = _style;
    }

    [[MusicAVPlayer shareMusicAVPlayer] playWithURL:self.songDetailModel];

    [self refreshEverything];

}

#pragma mark - 下一曲
- (void)nextSongButtonAction {
    
    /**< 顺序 */
    if ([MusicAVPlayer shareMusicAVPlayer].style == 0) {
        if ([MusicAVPlayer shareMusicAVPlayer].local == _songDetailModel.songs.count - 1) {
            [MusicAVPlayer shareMusicAVPlayer].local = 0;
        }else {
            [MusicAVPlayer shareMusicAVPlayer].local += 1;
        }
        [[MusicAVPlayer shareMusicAVPlayer] playWithURL:_songDetailModel];
        [MusicAVPlayer shareMusicAVPlayer].style = _style;
    }
    /**< 单曲 */
    if ([MusicAVPlayer shareMusicAVPlayer].style == 1) {
        if ([MusicAVPlayer shareMusicAVPlayer].local == _songDetailModel.songs.count - 1) {
            [MusicAVPlayer shareMusicAVPlayer].local = 0;
        }else {
            [MusicAVPlayer shareMusicAVPlayer].local += 1;
        }
        [[MusicAVPlayer shareMusicAVPlayer] playWithURL:_songDetailModel];
        [MusicAVPlayer shareMusicAVPlayer].style = _style;
    }
    /**< 随机 */
    if ([MusicAVPlayer shareMusicAVPlayer].style == 2) {
        [MusicAVPlayer shareMusicAVPlayer].local = arc4random()%_songDetailModel.songs.count;
        [[MusicAVPlayer shareMusicAVPlayer] playWithURL:_songDetailModel];
        [MusicAVPlayer shareMusicAVPlayer].style = _style;
    }
    /**< 刷新按钮图片 */
    if ([MusicAVPlayer shareMusicAVPlayer].isPlay) {
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"musicStop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
    }

    //    刷新页面数据
    [self refreshEverything];
    
}

#pragma mark - 播放列表按钮点击事件
- (void)listButtonAction:(UIButton *)sender {
   
}


#pragma mark ------ 下一曲的时候刷新页面，歌名，歌手名，图片 ------
- (void)refreshEverything {
    
    _songLabel.text = [MusicAVPlayer shareMusicAVPlayer].songDetailModel.name;
    _singerLabel.text = [MusicAVPlayer shareMusicAVPlayer].songDetailModel.singerName;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:[MusicAVPlayer shareMusicAVPlayer].songDetailModel.picUrl]];

}
#pragma mark - ------------ 重写set方法赋值 ---------------

- (void)setCommandModel:(RankListSongListSongsModel *)commandModel {
    
    if (_commandModel != commandModel) {
        [_commandModel release];
        _commandModel = [commandModel retain];
    }
    
    _songLabel.text = commandModel.name;
    _singerLabel.text = commandModel.singerName;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:commandModel.picUrl]];
    
}

- (void)setSongDetailModel:(SongListSongsModel *)songDetailModel {
    if (_songDetailModel != songDetailModel) {
        [_songDetailModel release];
        _songDetailModel = [songDetailModel retain];
    }
//    NSLog(@"%@", _songDetailModel);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self imageRound];
}

#pragma mark - ------------ 转呀转呀┗|｀O′|┛ ~~ ---------------

- (void)imageRound {
    
    CABasicAnimation *animation = [ CABasicAnimation animationWithKeyPath: @"transform" ];
    
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    
    //围绕Z轴旋转，垂直与屏幕
    animation.toValue = [ NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 0.0, 1.0) ];
    animation.duration = 10.0;
    
    //旋转效果累计，先转180度，接着再旋转180度，从而实现360旋转
    animation.cumulative = YES;
    animation.repeatCount = NSIntegerMax;
    
    [self.imageView.layer setCornerRadius:CGRectGetHeight([self.imageView bounds]) / 2];
    self.imageView.layer.masksToBounds = YES;
    [self.imageView.layer addAnimation:animation forKey:nil];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
